package com.woniu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.domain.Company;
import com.woniu.domain.Feedback;
import com.woniu.domain.Position;
import com.woniu.dto.Result;
import com.woniu.dto.StatusCode;
import com.woniu.service.CompanyService;
import com.woniu.service.PositionService;
import com.woniu.vo.AllCompanyConditionVo;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.domain.Company;
import com.woniu.domain.User;
import com.woniu.dto.Result;
import com.woniu.dto.StatusCode;
import com.woniu.service.CompanyService;
import com.woniu.vo.UserAndCompany;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyy
 * @since 2021-03-05
 */
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Resource
    private CompanyService companyService;

    //公司注册
    @PostMapping("companyRegister")
    public Result companyRegister(@RequestBody UserAndCompany company){
        System.out.println(company);
        companyService.addCompany(company);
        return new Result(true, StatusCode.OK,"公司注册成功");
    }

    //登录后展示公司信息（根据招聘者id查询公司信息）
    @GetMapping("getCompany/{uid}")
    public Result getCompany(@PathVariable Integer uid){
        Company company = companyService.queryCompanyByUid(uid);
        return new Result(true,StatusCode.OK,"公司信息查询成功",company);
    }

    //修改公司信息
    @PostMapping("updateCompany")
    public Result updateCompany(@RequestBody UserAndCompany company){
        System.out.println(company);
        companyService.updateCompany(company);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    //根据公司id查询该公司所有的招聘者
    @GetMapping("queryStaff/{compId}")
    public Result queryStaff(@PathVariable Integer compId){
        List<User> users = companyService.queryStaff(compId);
        return new Result(true,StatusCode.OK,"查询成功",users);
    }

    //移除公司的员工
    @GetMapping("deleteStaff/{uid}/{compId}")
    public Result deleteStaff(@PathVariable Integer uid,@PathVariable Integer compId){
        System.out.println(uid);
        companyService.deleteStaff(uid,compId);
        return new Result(true,StatusCode.OK,"移除成功");
    }

    //邀请员工加入招聘（根据手机号添加招聘者）
    @GetMapping("addStaff/{tel}/{compId}")
    public Result addStaff(@PathVariable String tel,@PathVariable Integer compId){
        System.out.println(compId);
        boolean bool = companyService.addStaff(tel, compId);
        if(bool){
            return new Result(true,StatusCode.OK,"邀请成功");
        }
        return new Result(false,StatusCode.UnknownAccount,"用户未注册");
    }

    //根据公司名称查询公司是否注册
    @GetMapping("checkCompanyName/{name}")
    public Result checkCompanyName(@PathVariable String name){
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        Company company = companyService.getOne(wrapper);
        if (ObjectUtils.isEmpty(company)) {
            return new Result(true,StatusCode.OK,"公司未注册");
        }else{
            return new Result(false,StatusCode.MUCHNAME,"该公司已被注册");
        }

    }

    @Resource
    private PositionService positionService;

    @GetMapping("getCompanies")
    public Result getAllCompanies(AllCompanyConditionVo allCompanyConditionVo){
       IPage<Company> pages= companyService.companiesPage(allCompanyConditionVo);
        return new Result(true, StatusCode.OK,"查询所有公司成功",pages);
    }

    @GetMapping("getOneCompany/{cid}")
    public Result getOneCompany(@PathVariable Integer cid){
        Company company = companyService.getById(cid);
        return new Result(true,StatusCode.OK,"查看单个公司信息成功",company);
    }

    @PutMapping("sendHotCompany")
    public Result sendHotCompany(@RequestBody Company company){
        List<Position> positions = positionService.deletePositinByCompanyId(company.getCompanyId());

        positions.forEach(position -> {
            position.setHot("平台推送");
            positionService.updateById(position);
        });


        company.setHot("平台推送");
        companyService.updateById(company);
        return new Result(true,StatusCode.OK,"推送热门公司成功");
    }

    @PutMapping("updateCompanyStatus")
    public Result updateCompanyStatus(@RequestBody Company company){
        System.out.println(company);
        company.setStatus("已通过");
        companyService.updateById(company);
        return new Result(true,StatusCode.OK,"通过审核");
    }

    @DeleteMapping("deleteCompanyById/{cid}")
    public Result deleteCompanyById(@PathVariable Integer cid){
        companyService.deleteCompanyById(cid);
        return new Result(true,StatusCode.OK,"删除该公司及其相关信息成功");
    }

}

